<?php
namespace app\navbar\event;

use app\common\controller\Addon;

class Admin extends Addon
{
	public function _initialize()
    {
		parent::_initialize();
	}
    
    //定义表单字段列表
    protected function fields($data=[])
    {
        return model('navbar/Common','loglic')->fields($data);
    }
    
    //定义表单初始数据
    protected function formData()
    {
        if( $id = input('id/d',0) ){
            return navbarId($id);
		}
        return [];
    }
    
    //定义表格数据（JSON）
    protected function ajaxJson()
    {
        $args = array();
        $args['cache']   = false;
        $args['result']  = 'level';
        $args['type']    = 'navs';
        //$args['module']  = 'navbar';
        //$args['limit']   = DcEmpty($this->query['pageSize'], 30);
        //$args['page']    = DcEmpty($this->query['pageNumber'], 1);
        $args['sort']    = DcEmpty($this->query['sortName'], 'term_order');
        $args['order']   = DcEmpty($this->query['sortOrder'], 'desc');
        $args['search']  = $this->query['searchText'];
        //URL参数不同条件
        if($this->query['sortName']){
           $args['result']  = 'array'; 
        }
        //扩展字段生成查询参数
        $args['meta_query']    = [];
        foreach( DcArrayFilter($this->query, config('custom_fields.term_meta')) as $key=>$value ){
            if($value){
                array_push($args['meta_query'], ['key'=>['eq',$key],'value'=>['eq',$value]]);
            }
        }
        //自定义查询条件
        if( $where = DcWhereQuery(['term_module','term_controll','term_action','term_status'], 'eq', $this->query) ){
            $args['where'] = $where;
        }
        //查询数据
        $list = DcTermSelect($args);
        //数据返回
        return DcEmpty($list,[]);
    }
    
    public function save()
    {
        $id = navbarSave(input('post.'));
		if($id < 1){
			$this->error(\daicuo\Term::getError());
		}
		$this->success(lang('success'));
    }
    
    public function delete()
    {
        $ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        $result = navbarDelete($ids);
        $this->success(lang('success'));
    }
    
    public function update()
    {
        $data = input('post.');
        if(!$data['term_id']){
            $this->error(lang('mustIn'));
        }
        if( !navbarUpdate($data) ){
            $this->error(\daicuo\Term::getError());
        }
        $this->success(lang('success'));
    }
    
    public function preview()
    {
        if( !$id = input('id/d',0) ){
            $this->error(lang('mustIn'));
        }
        //快速查询数据
        $term = navbarId($id);
        if(!$term['navs_url']){
            $this->error(lang('empty'));
        }
        //去掉后台入口文件
        $redirect = str_replace($this->request->baseFile(), '', navbarUrl($term));
        //跳转至链接地址
        $this->redirect($redirect, 302);
    }
}